package com.qbw.app.model;

import androidx.databinding.Bindable;

import com.qbw.app.BR;
import com.qbw.app.module.vehicle.VehicleDataBindingAdapter;

import java.io.Serializable;
import java.util.ArrayList;

/**
 * @author QBW
 * @date 2019/9/26
 */
public class ElectricArea extends BaseModel implements Serializable {

    private int fenceId;
    private String fenceName;
    /**
     * 围栏状态：0关闭 1开启
     */
    private int fenceState;
    private int vehicleCount;
    private String triggerSet;

    public int getFenceId() { return fenceId;}

    public void setFenceId(int fenceId) { this.fenceId = fenceId;}

    @Bindable
    public String getFenceName() { return fenceName;}

    public void setFenceName(String fenceName) { this.fenceName = fenceName;}

    @Bindable
    public int getFenceState() { return fenceState;}

    public void setFenceState(int fenceState) { this.fenceState = fenceState;}

    @Bindable
    public int getVehicleCount() { return vehicleCount;}

    public void setVehicleCount(int vehicleCount) { this.vehicleCount = vehicleCount;}

    public String getTriggerSet() {
        return triggerSet;
    }

    public void setTriggerSet(String triggerSet) {
        this.triggerSet = triggerSet;
    }

    public static class List extends BaseModelList<ElectricArea> {}

    public static class ResponseList extends BaseResponse<List> {}

    public static class Mode extends BaseModel implements Serializable {
        /**
         * 1,in;2,out
         */
        private int state;
        private boolean selected = true;

        public Mode() {
        }

        public Mode(int state) {
            this.state = state;
        }

        @Bindable
        public int getState() {
            return state;
        }

        public void setState(int state) {
            this.state = state;
        }

        @Bindable
        public boolean isSelected() {
            return selected;
        }

        public void setSelected(boolean selected) {
            this.selected = selected;
            notifyPropertyChanged(BR.selected);
        }

        public static class ModeList extends BaseModelList<Mode> {

            public ModeList getSelectedModeList() {
                ModeList mlist = new ModeList();
                mlist.setList(new ArrayList<>());
                int size = size();
                Mode mode;
                for (int i = 0; i < size; i++) {
                    mode = getList().get(i);
                    if (mode.isSelected()) {
                        mlist.getList().add(mode);
                    }
                }
                return mlist;
            }

            public String getSlectedModeNames() {
                ModeList ms = getSelectedModeList();
                StringBuilder sb = new StringBuilder();
                if (ms.size() > 0) {
                    for (Mode m : ms.getList()) {
                        if (m.isSelected()) {
                            sb.append(VehicleDataBindingAdapter.getElectricAreaModeByState(m.getState()))
                              .append(",");
                        }
                    }
                }
                if (sb.length() > 0) {
                    sb.deleteCharAt(sb.length() - 1);
                }
                return sb.toString();
            }

            public String getSlectedModeParams() {
                ModeList ms = getSelectedModeList();
                StringBuilder sb = new StringBuilder();
                if (ms.size() > 0) {
                    for (Mode m : ms.getList()) {
                        if (m.isSelected()) {
                            sb.append(m.getState()).append("#");
                        }
                    }
                }
                if (sb.length() > 0) {
                    sb.deleteCharAt(sb.length() - 1);
                }
                return sb.toString();
            }
        }
    }
}
